home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: lion.cs.latrobe.edu.au!boylesgj
- From: boylesgj@lion.cs.latrobe.edu.au (Gregary J Boyles)
- Subject: Operator overload problem!
- X-Nntp-Posting-Host: lion.cs.latrobe.edu.au
- Message-ID: <DoIn1z.Gou@latcs1.lat.oz.au>
- Sender: news@latcs1.lat.oz.au (news)
- Organization: Comp.Sci & Comp.Eng, La Trobe Uni, Australia
- Date: Tue, 19 Mar 1996 13:05:58 GMT
-
- I am trying to overload the << but I get the syntax errors : friends must be functions or
- classes, 'ostream' cannot start a parameter declaration, 'Address::operator <<(int &,Address &)'
- must be declared at the first set of astericks and declaration syntax error at the second set.
-
- What the #$%& is it on about? I copied the operator overload function directly out of a program
- which compiles and runs so why all of a sudden won't the bloody compiler accept it?
-
-
- #include "defines.h"
-
- class Address
- {
- private : int Number;
- char *Street;
- char *City;
- int Zip;
-
- public : // Constructors
- Address(int ANumber,const char *AStreet,const char *ACity,int Zip);
- Address();
- // Copy constructor
- Address(Address& AnAddress);
- // Deconstructor
- ~Address();
- // Operator overloads
-
- **************************************************************************
- * *
- * friend ostream& operator <<(ostream& OutPutStream,Address& AnAddress); *
- * *
- **************************************************************************
-
- // Functions
- void Change(int ANumber,const char *AStreet,const char *ACity,int AZip);
- };
-
- #endif
-
-
-
-
-
-
-
- // address.cpp
-
- #include "address.h"
- #include <string.h>
-
- // Constructors
- Address::Address(int ANumber,const char *AStreet,const char *ACity,int AZip)
- {
- Number=ANumber;
- Street=new char[strlen(AStreet)+1];
- strcpy(Street,AStreet);
- City=new char[strlen(ACity)+1];
- strcpy(City,ACity);
- Zip=AZip;
- }
-
- Address::Address()
- {
- Number=0;
- Street=new char[strlen("")+1];
- strcpy(Street,"");
- City=new char[strlen("")+1];
- strcpy(City,"");
- Zip=0;
- }
-
- // Copy constructor
- Address::Address(Address& AnAddress)
- {
- Number=AnAddress.Number;
- Street=new char[strlen(AnAddress.Street)+1];
- strcpy(Street,AnAddress.Street);
- City=new char[strlen(AnAddress.City)+1];
- strcpy(City,AnAddress.City);
- Zip=AnAddress.Zip;
- }
-
- // Deconstructor
- Address::~Address()
- {
- Number=0;
- strcpy(Street,"");
- strcpy(City,"");
- Zip=0;
- }
-
- // Operator overloads
-
- ***************************************************************
- *
- ostream& operator <<(ostream& OutPutStream,Address& AnAddress)*
- *
- ***************************************************************
-
- {
- OutPutStream<<"Street : "<<AnAddress.Number<<" "<<AnAddress.Street<<EOLN;
- OutPutStream<<"City : "<<AnAddress.City<<EOLN;
- OutPutStream<<"Zip code : "<<AnAddress.Zip<<EOLN;
- }
-
- // Functions
- void Address::Change(int ANumber,const char *AStreet,const char *ACity,int AZip)
- {
- Number=ANumber;
- delete Street;
- Street=new char[strlen(AStreet)+1];
- strcpy(Street,AStreet);
- delete City;
- City=new char[strlen(ACity)+1];
- strcpy(City,ACity);
- Zip=AZip;
- }
-
-